﻿using System;
using NHibData.Repository;
using NHibernate;
using System.Web;

namespace NHibData.SessionManager
{
    /// <summary>
    /// Implement interface for storing values in HttpContext
    /// </summary>
    internal class HttpContextSessionManager : INHibenraneSessionManager
    {
        public ISession NHibernateSession
        {
            get
            {
                return HttpContext.Current.Items[NHibernateHelper.DefaultNHibernateConfigName] as ISession;
            }
            set
            {
                HttpContext.Current.Items[NHibernateHelper.DefaultNHibernateConfigName] = value;
            }
        }

        public ISession GetSessionByKey(string nHibernareConfigName)
        {
            if(string.IsNullOrEmpty(nHibernareConfigName))
                throw new ArgumentNullException("nHibernareConfigName");

            return HttpContext.Current.Items[nHibernareConfigName] as ISession;
        }

        public void SetSessionByKey(string nHibernareConfigName, ISession session)
        {
            if (string.IsNullOrEmpty(nHibernareConfigName))
                throw new ArgumentNullException("nHibernareConfigName");

            HttpContext.Current.Items[nHibernareConfigName] = session;
        }
    }
}